#include "PlayState.h"

PlayState::PlayState (GameContext& gc) : GameState (gc)
{}
PlayState::~PlayState ()
{}

void PlayState::Load ()
{
	mCharacter.Load ();
	mCharacter.SetPosition (SVector2 (100.0f, 100.0f));

	char levelName [256];
	char textureName [256];
	sprintf_s (levelName, 256, "level0%d.txt", mGameContext.GetLevel ());
	sprintf_s (textureName, 256, "texturepack0%d.txt", mGameContext.GetLevel ());
	mMap.Load (levelName, textureName);
}
void PlayState::Unload ()
{
	mCharacter.Unload ();
	mMap.Unload ();
}
NextState PlayState::Update (float deltaTime)
{
	mCharacter.Update (deltaTime, mMap);

	NextState nextState = None;
	if (Input_IsKeyPressed (Keys::ESCAPE))
	{
		nextState = Frontend;
	}

	return nextState;
}
void PlayState::Render ()
{
	const int kWinHeight = IniFile_GetInt ("WinHeight", 600);
	const int kWinWidth  = IniFile_GetInt ("WinWidth", 800);
	SVector2 viewTarget  = mCharacter.GetPosition ();
	SVector2 viewOffset;

	viewOffset.x = viewTarget.x - (kWinWidth * 0.5f);
	viewOffset.y = viewTarget.y - (kWinHeight * 0.5f);

	SVector2 limit;

	limit.x = (float)(mMap.GetWidth () - kWinWidth);
	limit.y = (float)(mMap.GetHeight () - kWinHeight);

	viewOffset.x = Clamp (viewOffset.x, 0.0f, limit.x);
	viewOffset.y = Clamp (viewOffset.y, 0.0f, limit.y);

	mMap.Render (viewOffset);
	mCharacter.Render (viewOffset);
}